package com.example.myweb.Controller.Auth;

import com.example.myweb.Common.ApiResponse;
import com.example.myweb.Domain.Entity.User;

import com.example.myweb.Dto.CollectionRequestDTO;
import com.example.myweb.Service.Auth.CollectionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Map;

@RestController
@RequestMapping("/collections")
public class CollectionController {

    @Autowired
    private CollectionService collectionService;

    /**
     * 收藏一篇文章
     * POST /collections
     */
    @PostMapping
    public ResponseEntity<ApiResponse<Void>> createCollection(
            // 使用DTO和@Valid注解
            @Valid @RequestBody CollectionRequestDTO request,
            @AuthenticationPrincipal User currentUser) {

        // 不再需要手动检查null，@Valid会处理
        collectionService.createCollection(currentUser, request.getArticleId());
        return new ResponseEntity<>(ApiResponse.success(null, "文章收藏成功"), HttpStatus.CREATED);
    }

    /**
     * 取消收藏一篇文章
     * DELETE /collections/{articleId}
     */
    @DeleteMapping("/{articleId}")
    public ResponseEntity<ApiResponse<Void>> deleteCollection(
            @PathVariable Long articleId,
            @AuthenticationPrincipal User currentUser) {

        collectionService.deleteCollection(currentUser, articleId);
        return ResponseEntity.ok(ApiResponse.success(null, "文章取消收藏成功"));
    }
}